/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.graph;

import java.awt.BasicStroke;
import java.awt.Graphics2D;

import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides the implementation of drawing-related primitives.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Draw {

    /**
     * No instance of this class.
     */
    private Draw() {
    } // end empty constructor

    /**
     * Sets a pixel to the current color.
     * @param ctxt context
     * @param vx x-component of the pixel coordinate
     * @param vy y-component of the pixel coordinate
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_plot(final CodeRunner ctxt,
                                     final Value vx,
                                     final Value vy)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        final int x = vx.asLong();
        final int y = slot.convertY(vy.asLong());
        final Graphics2D win = slot.createWindowGraphics();
        if (win != null) {
            win.setStroke(new BasicStroke(1.0f));
            win.drawLine(x, y, x, y);
            win.dispose();
            slot.repaint();
        } // end if
        final Graphics2D store = slot.createBackingStoreGraphics();
        if (store != null) {
            store.setStroke(new BasicStroke(1.0f));
            store.drawLine(x, y, x, y);
            store.dispose();
        } // end if
        return Value.UNIT;
    } // end method 'caml_gr_plot(CodeRunner, Value, Value)'

    /**
     * Changes the current position.
     * @param ctxt context
     * @param vx x-component of the new position
     * @param vy y-component of the new position
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_moveto(final CodeRunner ctxt,
                                       final Value vx,
                                       final Value vy)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        slot.setX(vx.asLong());
        slot.setY(vy.asLong());
        return Value.UNIT;
    } // end method 'caml_gr_moveto(CodeRunner, Value, Value)'

    /**
     * Returns the x-component of the current position.
     * @param ctxt context
     * @param unit ignored
     * @return the x-component of the current position
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_current_x(final CodeRunner ctxt,
                                          final Value unit)
        throws Fail.Exception {
        return Value.createFromLong(GraphSlot.checkGraph(ctxt).getX());
    } // end method 'caml_gr_current_x(CodeRunner, Value)'

    /**
     * Returns the y-component of the current position.
     * @param ctxt context
     * @param unit ignored
     * @return the y-component of the current position
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_current_y(final CodeRunner ctxt,
                                          final Value unit)
        throws Fail.Exception {
        return Value.createFromLong(GraphSlot.checkGraph(ctxt).getY());
    } // end method 'caml_gr_current_y(CodeRunner, Value)'

    /**
     * Draws a line from the current position to the given point. <br/>
     * The current position is set to the given point.
     * @param ctxt context
     * @param vx x-component of line destination
     * @param vy y-component of line destination
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_lineto(final CodeRunner ctxt,
                                       final Value vx,
                                       final Value vy)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        final int x0 = slot.getX();
        final int y0 = slot.convertY(slot.getY());
        final int x1 = vx.asLong();
        final int y1 = vy.asLong();
        final Graphics2D win = slot.createWindowGraphics();
        if (win != null) {
            win.drawLine(x0, y0, x1, slot.convertY(y1));
            win.dispose();
            slot.repaint();
        } // end if
        final Graphics2D store = slot.createBackingStoreGraphics();
        if (store != null) {
            store.drawLine(x0, y0, x1, slot.convertY(y1));
            store.dispose();
        } // end if
        slot.setX(x1);
        slot.setY(y1);
        return Value.UNIT;
    } // end method 'caml_gr_lineto(CodeRunner, Value, Value)'

    /**
     * Draws a rectangle.
     * @param ctxt context
     * @param vx x-component of lower left corner coordinate
     * @param vy y-component of lower left corner coordinate
     * @param vw width
     * @param vh height
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_draw_rect(final CodeRunner ctxt,
                                          final Value vx,
                                          final Value vy,
                                          final Value vw,
                                          final Value vh)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        final int w = vw.asLong();
        final int h = vh.asLong();
        final int x = vx.asLong();
        final int y = slot.convertY(vy.asLong()) - h;
        final Graphics2D win = slot.createWindowGraphics();
        if (win != null) {
            win.drawRect(x, y, w, h);
            win.dispose();
            slot.repaint();
        } // end if
        final Graphics2D store = slot.createBackingStoreGraphics();
        if (store != null) {
            store.drawRect(x, y, w, h);
            store.dispose();
        } // end if
        return Value.UNIT;
    } // end method 'caml_gr_draw_rect(CodeRunner, Value, Value, ...)'

    /**
     * Draws an elliptical arc.
     * @param ctxt context
     * @param vx x-component of center
     * @param vy y-component of center
     * @param vrx horizontal radius
     * @param vry vertical radius
     * @param va1 starting angle
     * @param va2 ending angle
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_draw_arc(final CodeRunner ctxt,
                                         final Value vx,
                                         final Value vy,
                                         final Value vrx,
                                         final Value vry,
                                         final Value va1,
                                         final Value va2)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        final int rx = vrx.asLong();
        final int ry = vry.asLong();
        final int x = vx.asLong() - rx;
        final int y = slot.convertY(vy.asLong()) - ry;
        final int w = rx * 2;
        final int h = ry * 2;
        final int sa = va1.asLong();
        final int aa = va2.asLong() - sa;
        final Graphics2D win = slot.createWindowGraphics();
        if (win != null) {
            win.drawArc(x, y, w, h, sa, aa);
            win.dispose();
            slot.repaint();
        } // end if
        final Graphics2D store = slot.createBackingStoreGraphics();
        if (store != null) {
            store.drawArc(x, y, w, h, sa, aa);
            store.dispose();
        } // end if
        return Value.UNIT;
    } // end method 'caml_gr_draw_arc(CodeRunner, Value, Value, ...)'

    /**
     * Exact synonym of {@link #caml_gr_draw_arc(CodeRunner, Value, Value, Value, Value, Value, Value)}.
     */
    @Primitive
    public static Value caml_gr_draw_arc_nat(final CodeRunner ctxt,
                                             final Value vx,
                                             final Value vy,
                                             final Value vrx,
                                             final Value vry,
                                             final Value va1,
                                             final Value va2)
        throws Fail.Exception {
        return caml_gr_draw_arc(ctxt, vx, vy, vrx, vry, va1, va2);
    } // end method 'caml_gr_draw_arc_nat(CodeRunner, Value, Value, Value, Value, Value, Value)'


    /**
     * Changes the line width.
     * @param ctxt context
     * @param lw new line width
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_set_line_width(final CodeRunner ctxt,
                                               final Value lw)
        throws Fail.Exception {
        GraphSlot.checkGraph(ctxt).setLineWidth(lw.asLong());
        return Value.UNIT;
    } // end method 'caml_gr_set_line_width(CodeRunner, Value)'

} // end class 'Draw'
